/*
   
    Copyright (C) 2011  Antoine

        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 3 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package subs;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.border.MatteBorder;

public class MainView extends JFrame {

	protected JTabbedPane		jtp;
	protected JEditorPane		editorPane;
	protected JPanel			panel;
	protected JPanel			panel_sync;
	protected JTable			table;
	protected JTextField		debut_sub;
	protected JMenuItem			enregistrer;
	protected JMenuItem			enregistrer_sous;
	protected JSpinner			duree_sub;
	protected JEditorPane		editor_texte;
	protected JPanel			panel_modify;
	protected JTextArea			duree_area;
	protected DefaultTableModel	model;
	protected JMenuItem			find;

	public MainView() {

		super("Java Sub Editor");

		String os = System.getProperty("os.name").toLowerCase();

		// TabbedPane
		jtp = new JTabbedPane();
		getContentPane().add(jtp);

		// Table
		table = new JTable();
		// Attributs de la JTable
		String[] columnNames = { "ID", "Debut", "Fin", "Texte" };

		// Declaration JTable
		DefaultTableModel model = new DefaultTableModel() {

			@Override
			public boolean isCellEditable(int row, int column) {
				// all cells false
				return false;
			}
		};

		table.setModel(model);
		model.setColumnIdentifiers(columnNames);
		model.setRowCount(50);

		TableController tableController1 = new TableController();
		// table.setCellSelectionEnabled(true);
		table.getSelectionModel().addListSelectionListener(tableController1);
		JScrollPane scrollPane = new JScrollPane(table);
		jtp.add("Table", scrollPane);

		// editor
		editorPane = new JEditorPane();
		editorPane.setEditable(false);

		JScrollPane editorScrollPane = new JScrollPane(editorPane);
		editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		jtp.add("Preview", editorScrollPane);

		// JMenuBar
		JMenuBar menuBar = new JMenuBar();

		// JMenu
		JMenu fileMenu = new JMenu("File");
		JMenu editMenu = new JMenu("Edit");
		JMenu helpMenu = new JMenu("Help");
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);

		// fileMenu
		JMenuItem ouvrir = new JMenuItem("Open");
		find = new JMenuItem("Find");
		find.setEnabled(false);
		JMenuItem quitter = new JMenuItem("Quit");
		enregistrer = new JMenuItem("Save");
		enregistrer.setEnabled(false);
		enregistrer_sous = new JMenuItem("Save As...");
		enregistrer_sous.setEnabled(false);
		enregistrer_sous.setActionCommand("save_as");

		// helpMenu
		JMenuItem propos = new JMenuItem("About");

		// Raccourci clavier
		ouvrir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		enregistrer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		enregistrer_sous.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() + KeyEvent.SHIFT_MASK));
		if (os.indexOf("mac") == -1) {
			quitter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		}

		// fileMenu
		editMenu.add(find);
		fileMenu.add(ouvrir);
		fileMenu.add(enregistrer);
		fileMenu.add(enregistrer_sous);
		if (os.indexOf("mac") == -1) {
			fileMenu.add(quitter);
		}

		// helpMenu
		helpMenu.add(propos);

		// Réaction équivalente pour le bouton image sync
		panel_modify = new JPanel();
		panel_modify.setVisible(false);
		getContentPane().add(panel_modify, BorderLayout.SOUTH);
		GridBagLayout gbl_panel_modify = new GridBagLayout();
		gbl_panel_modify.columnWidths = new int[] { 50, 134, 50, 50, 134, 100, 0, 50, 181, 61, 0 };
		gbl_panel_modify.rowHeights = new int[] { 0, 30, 0, 0, 0 };
		gbl_panel_modify.columnWeights = new double[] { 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_panel_modify.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		panel_modify.setLayout(gbl_panel_modify);

		JLabel lblDbut = new JLabel("Start");
		GridBagConstraints gbc_lblDbut = new GridBagConstraints();
		gbc_lblDbut.anchor = GridBagConstraints.EAST;
		gbc_lblDbut.insets = new Insets(0, 0, 5, 5);
		gbc_lblDbut.gridx = 0;
		gbc_lblDbut.gridy = 0;
		panel_modify.add(lblDbut, gbc_lblDbut);

		debut_sub = new JTextField();
		debut_sub.setActionCommand("debut_sub");
		GridBagConstraints gbc_debut_sub = new GridBagConstraints();
		gbc_debut_sub.anchor = GridBagConstraints.WEST;
		gbc_debut_sub.insets = new Insets(0, 0, 5, 5);
		gbc_debut_sub.gridx = 1;
		gbc_debut_sub.gridy = 0;
		panel_modify.add(debut_sub, gbc_debut_sub);
		debut_sub.setColumns(10);

		JLabel lblDure = new JLabel("Length");
		GridBagConstraints gbc_lblDure = new GridBagConstraints();
		gbc_lblDure.insets = new Insets(0, 0, 5, 5);
		gbc_lblDure.gridx = 2;
		gbc_lblDure.gridy = 0;
		panel_modify.add(lblDure, gbc_lblDure);

		duree_sub = new JSpinner();
		// duree_sub.addChangeListener(new SpinnerListener(this));
		duree_sub.setModel(new SpinnerNumberModel(0, 0, 5000, 20));
		GridBagConstraints gbc_duree_sub = new GridBagConstraints();
		gbc_duree_sub.insets = new Insets(0, 0, 5, 5);
		gbc_duree_sub.gridx = 3;
		gbc_duree_sub.gridy = 0;
		panel_modify.add(duree_sub, gbc_duree_sub);

		String[] choix_box_1 = { "Augmenter", "Diminuer" };

		JLabel lblms = new JLabel("(ms)");
		GridBagConstraints gbc_lblms = new GridBagConstraints();
		gbc_lblms.insets = new Insets(0, 0, 5, 5);
		gbc_lblms.anchor = GridBagConstraints.WEST;
		gbc_lblms.gridx = 4;
		gbc_lblms.gridy = 0;
		panel_modify.add(lblms, gbc_lblms);
		JComboBox choix_combo = new JComboBox(choix_box_1);
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 8;
		gbc_comboBox.gridy = 0;
		panel_modify.add(choix_combo, gbc_comboBox);

		JLabel lblTexte = new JLabel("Text");
		GridBagConstraints gbc_lblTexte = new GridBagConstraints();
		gbc_lblTexte.anchor = GridBagConstraints.EAST;
		gbc_lblTexte.insets = new Insets(0, 0, 5, 5);
		gbc_lblTexte.gridx = 0;
		gbc_lblTexte.gridy = 1;
		panel_modify.add(lblTexte, gbc_lblTexte);

		editor_texte = new JEditorPane();
		JScrollPane scroll_editor = new JScrollPane(editor_texte);
		scroll_editor.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll_editor.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		editor_texte.setCaretColor(UIManager.getColor("EditorPane.inactiveForeground"));
		editor_texte.setBorder(UIManager.getBorder("TextField.border"));
		GridBagConstraints gbc_editor_texte = new GridBagConstraints();
		gbc_editor_texte.gridheight = 3;
		gbc_editor_texte.fill = GridBagConstraints.BOTH;
		gbc_editor_texte.gridwidth = 3;
		gbc_editor_texte.insets = new Insets(0, 0, 0, 5);
		gbc_editor_texte.gridx = 1;
		gbc_editor_texte.gridy = 1;
		panel_modify.add(scroll_editor, gbc_editor_texte);

		JButton modify = new JButton("Modify");
		modify.setActionCommand("Change");
		GridBagConstraints gbc_modify = new GridBagConstraints();
		gbc_modify.gridheight = 3;
		gbc_modify.insets = new Insets(0, 0, 0, 5);
		gbc_modify.gridx = 4;
		gbc_modify.gridy = 1;
		panel_modify.add(modify, gbc_modify);

		duree_area = new JTextArea();
		duree_area.setBorder(UIManager.getBorder("TextField.border"));
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.insets = new Insets(0, 0, 5, 5);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 8;
		gbc_textArea.gridy = 1;
		panel_modify.add(duree_area, gbc_textArea);

		ControllerSync cs = new ControllerSync(choix_combo, duree_area, this);

		JLabel lblSeconds = new JLabel("Second(s)");
		GridBagConstraints gbc_lblSeconds = new GridBagConstraints();
		gbc_lblSeconds.insets = new Insets(0, 0, 5, 0);
		gbc_lblSeconds.gridx = 9;
		gbc_lblSeconds.gridy = 1;
		panel_modify.add(lblSeconds, gbc_lblSeconds);

		JButton btnSync = new JButton("Sync");
		btnSync.setActionCommand("Synchroniser");

		GridBagConstraints gbc_btnSync = new GridBagConstraints();
		gbc_btnSync.anchor = GridBagConstraints.EAST;
		gbc_btnSync.insets = new Insets(0, 0, 5, 0);
		gbc_btnSync.gridx = 9;
		gbc_btnSync.gridy = 2;
		panel_modify.add(btnSync, gbc_btnSync);
		btnSync.addActionListener(cs);

		// Ajout reaction clic
		MainController controller1 = new MainController(this);
		if (os.indexOf("mac") == -1) {
			quitter.addActionListener(controller1);
		}
		enregistrer.addActionListener(controller1);
		find.addActionListener(controller1);
		ouvrir.addActionListener(controller1);
		propos.addActionListener(controller1);
		enregistrer_sous.addActionListener(controller1);
		modify.addActionListener(controller1);

		///ControllerMouse controller_mouse1 = new ControllerMouse(this);

		//debut_sub.addMouseListener(controller_mouse1);

		// JFrame
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		Rectangle bounds = env.getMaximumWindowBounds();
		setBounds(bounds);
		setJMenuBar(menuBar);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(null);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				int retour = JOptionPane.showConfirmDialog(null, "Etes vous sur de vouloir quitter ?", "Information", JOptionPane.YES_NO_OPTION);
				if (retour == JOptionPane.YES_OPTION) {
					dispose();
				}
			}
		});
		setVisible(true);

	}

	public JPanel getJPanel() {
		return panel;
	}

	public JPanel getJPanel_sync() {
		return panel_sync;
	}

	public void setJPanel_sync(boolean bool) {
		panel_sync.setVisible(bool);
	}

	public JEditorPane getJEditorPane() {
		return editorPane;
	}

}
